package com.concerneyes.mine.Task;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.TabActivity.MyAdressActivity;
import com.concerneyes.mine.adapter.MyAdressAdapter;
import com.concerneyes.mine.model.MyAdressModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.AbsActivity;
import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;

/**
 * Created by Administrator on 2017/7/3 0003.
 */

public class AdressImpl {
    private Context context;
    private AdressListener listener;
    private AbsActivity absActivity;

    public AdressImpl(AbsActivity absActivity, AdressListener listener) {
        this.absActivity = absActivity;
        this.context = CEApp.getInstance();
        this.listener = listener;
    }

    /**
     * 地址列表
     */
    public void listAdress() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }
            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<MyAdressModel> model = null;
                if (TextUtils.isEmpty(arg1)) {
                    absActivity.showToast(R.string.network_error_tip);
                    return;
                }
                if (ret.getInt("code") == 200) {
                    try {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<MyAdressModel>>() {
                        }.getType());
                    } catch (Exception e) {
                    }
                } else {
                    absActivity.showToast(ret.getString("msg"));
                    listener.adressListFail();
                }
                if (model != null) {
                    listener.adressListSuccess(model);
                }else {
                    listener.adressListFail();
                }
            }
        };
        task.setDebug(false);
        task.excute(Url.BaseUrl + "/orderAddress/list", params, "POST");
    }

    /**
     * 删除地址
     *
     * @param id 对应地址的ID
     */
    public void deteleAdress(int id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("id", String.valueOf(id));
        params.put("uid", PreferUtil.getUserId());
        HttpAsyncTask task = new HttpAsyncTask() {

            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<MyAdressModel> model = null;
                if (ret.getInt("code") == 200) {
                    try {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<MyAdressModel>>() {
                        }.getType());
                    } catch (Exception e) {
                    }
                } else {
                    absActivity.showToast(ret.getString("msg"));
                    listener.deleteAdressFail();
                }
                if (model != null) {
                    listener.deleteAdressSuccess(model);
                }else {
                    listener.deleteAdressFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/orderAddress/delete", params, "POST");
    }

    /**
     * 设置为默认地址
     *
     * @param id 地址id
     */
    public void setDefaultAdress(int id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("id", String.valueOf(id));
        params.put("uid", PreferUtil.getUserId());
        HttpAsyncTask task = new HttpAsyncTask() {

            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<MyAdressModel> model = null;
                if (ret.getInt("code") == 200) {
                    try {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<MyAdressModel>>() {
                        }.getType());
                    } catch (Exception e) {
                    }
                } else {
                    absActivity.showToast(ret.getString("msg"));
                    listener.setDefaultAdressFail();
                }
                if (model != null) {
                    listener.setDefaultAdressSuccess(model);
                }else {
                    listener.setDefaultAdressFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/orderAddress/setDefault", params, "POST");
    }

    /**
     * 增加或修改地址
     * @param isAdd 是否为新增地址
     * @param id 需要修改的地址id
     * @param province 省
     * @param city 市
     * @param district 区县
     * @param address 详细地址
     * @param name 姓名
     * @param phone1 电话
     */
    public void addAdress(boolean isAdd, String id, String province, String city,
                           String district, String address, String name, String phone1) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        params.put("address", address);
        params.put("name", name);
        params.put("phone1", phone1);
        params.put("postCode", "100000");
        params.put("province", province);
        params.put("city", city);
        params.put("district", district);
        params.put("remark", "");
        if (!isAdd) {
            params.put("id", id);
        }
        Log.e("params", "" + params);
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                Log.e("arg1", "" + arg1);
                JsonMap ret = JsonMap.parseJson(arg1);
                if (TextUtils.isEmpty(arg1)) {
                    listener.addAdressFail();
                    return;
                }
                try {
                    if (ret.getInt("code") != 200) {
                        absActivity.showToast(ret.getString("msg"));
                        listener.addAdressFail();
                    } else {
                        listener.addAdressSuccess();
                    }
                } catch (Exception e) {
                    listener.addAdressFail();
                }
            }
        };
        task.setDebug(false);
        if (isAdd) {
            task.excute(Url.BaseUrl + "/orderAddress/add", params, "POST");
        } else {
            task.excute(Url.BaseUrl + "/orderAddress/update", params, "POST");
        }

    }
}
